package com.changgou.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.HttpClient;
import com.changgou.service.WeiXinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeiXinPayServiceImpl implements WeiXinPayService {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    // 微信统一下单的接口请求地址
    private static final String wxpay_api_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    // 微信查询支付状态的接口地址
    private static final String wxpay_status_url = "https://api.mch.weixin.qq.com/pay/orderquery";

    @Override
    public Map createNative(Map<String, String> params) {
        try {
            // 封装参数
            Map param = new HashMap();
            param.put("appid", appid);
            param.put("mch_id", partner);
            param.put("nonce_str", WXPayUtil.generateNonceStr());
            param.put("body", "畅购");
            param.put("out_trade_no", params.get("out_trade_no"));
            param.put("total_fee", params.get("total_fee"));
            param.put("spbill_create_ip", "127.0.0.1");
            param.put("notify_url", notifyurl);              // 回调地址
            param.put("trade_type", "NATIVE");

            param.put("attach", JSON.toJSONString(params));
            // 将参数转换成xml并携带签名
            String paramXml = WXPayUtil.generateSignedXml(param, partnerkey);
            // httpClient向微信发起请求
            HttpClient httpClient = new HttpClient(wxpay_api_url);

            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXml);
            // 发起https请求请求
            httpClient.post();

            // 获取返回的数据
            String content = httpClient.getContent();
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
            System.out.println("resultMap:" + resultMap);

            // 获取页面需要的参数
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("code_url", resultMap.get("code_url"));
            dataMap.put("out_trade_no", params.get("out_trade_no"));
            dataMap.put("total_fee", params.get("total_fee"));
            return dataMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询支付状态
     * @param out_trade_no
     * @return
     */
    @Override
    public Map queryStatus(String out_trade_no) {
        try {
            // 封装参数
            Map param = new HashMap();
            param.put("appid", appid);
            param.put("mch_id", partner);
            param.put("nonce_str", WXPayUtil.generateNonceStr());
            param.put("out_trade_no", out_trade_no);
            // 将参数转换成xml并携带签名
            String paramXml = WXPayUtil.generateSignedXml(param, partnerkey);
            // httpClient向微信发起请求
            HttpClient httpClient = new HttpClient(wxpay_status_url);
            httpClient.setHttps(true);
            httpClient.setXmlParam(WXPayUtil.mapToXml(param));
            httpClient.post();

            String content = httpClient.getContent();
            Map<String, String> map = WXPayUtil.xmlToMap(content);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
